$(function () {
    $("#jqGrid").jqGrid({
        url: '../custombaseinfo/list',
        datatype: "json",
        colModel: [			
			{ label: '单位编码', name: 'unitId', index: 'unit_id', width: 50, key: true,hidden: true },
			{ label: '单位名称', name: 'unitName', index: 'unit_name', width: 80 }, 			
			{ label: '单位简码', name: 'unitCode', index: 'unit_code', width: 80 }, 			
			{ label: '单位性质(国有/合资/私营/其他)', name: 'unitNature', index: 'unit_nature', width: 80 }, 			
			{ label: '法人代表', name: 'legalPerson', index: 'legal_person', width: 80 }, 			
			{ label: '单位地址', name: 'unitAddress', index: 'unit_address', width: 80 }, 			
			{ label: '企业电话', name: 'unitTel', index: 'unit_tel', width: 80 }, 			
			{ label: '创建者', name: 'createBy', index: 'create_by', width: 80,hidden: true },
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80,hidden: true },
			{ label: '逻辑删除标记（0：显示；1：隐藏）', name: 'delFlag', index: 'del_flag', width: 80,hidden: true}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            status: null
        },
		showList: true,
		title: null,
		customBaseinfo: {},
        customVisit:{},
        visitStyleList:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.customBaseinfo = {};
		},
		update: function (event) {
			var unitId = getSelectedRow();
			if(unitId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(unitId)
		},
		saveOrUpdate: function (event) {
			var url = vm.customBaseinfo.unitId == null ? "../custombaseinfo/save" : "../custombaseinfo/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.customBaseinfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var unitIds = getSelectedRows();
			if(unitIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？',function(){
				$.ajax({
					type: "POST",
				    url: "../custombaseinfo/delete",
				    contentType: "application/json",
				    data: JSON.stringify(unitIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(unitId){
			$.get("../custombaseinfo/info/"+unitId, function(r){
                vm.customBaseinfo = r.customBaseinfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'unit_name': vm.q.status},
                page:page
            }).trigger("reloadGrid");
		},
        visit:function (event) {
            var unitId = getSelectedRow();
            if(unitId == null){
                return ;
            }
            this.getListinfo();
            var grid = $("#jqGrid");
            var rowKey = grid.getGridParam("selrow");
            var rowData = grid.jqGrid("getRowData",rowKey);
			// vm.customVisit.contactName = rowData.legalPerson;
            vm.customVisit = {
                unitName:rowData.unitName,
				unitId:rowData.unitId
            };

            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "添加拜访",
                area: ['800px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                	// console.log(vm.customVisit);
					vm.customVisit.visitTime = $("#visitTime").val();
                    var url = "../customvisit/save";
                    $.ajax({
                        type: "POST",
                        url: url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.customVisit),
                        success: function(r){
                            if(r.code === 0){
                                alert('操作成功', function(index2){
                                    // vm.reload();
                                    layer.close(index);
                                    window.location.href="custom/customvisit.html?unitName="+vm.customVisit.unitName;
                                });

                            }else{
                                alert(r.msg);
                            }
                        }
                    });

                }
            });
        },
        getListinfo: function(){
            $.get("../custombaseinfo/getListinfo", function(r){
                vm.visitStyleList = r.visit_style;
            });
        }




	}
});